package q153_findMin;

public class Solution_1 {
    public static void main(String[] args) {
        int[] a = {3,4,5,1,2};
        Solution_1 s = new Solution_1();
        System.out.println(s.findMin(a));
    }
    /*
    此题希望查找的是旋转后数组的最小值
    所以需要分情况讨论
    s1的解法有些臃肿 建议看s2
     */
    public int findMin(int[] nums) {
        if (nums.length == 1 || nums[0] < nums[nums.length - 1]) {
            return nums[0];
        }

        int low = 0, high = nums.length - 1, ans = Math.min(nums[low], nums[high]);
        while (high > low) {
//            if (nums[high] > nums[low]) {
//                ans = nums[low];
//                break;
//            }

            int mid = low + ((high - low) >> 1);
            if (nums[mid] > nums[low]) {
                ans = Math.min(ans, nums[high]);
                low = mid;
            }else if (nums[mid] < nums[high]) {
                ans = Math.min(ans, nums[mid]);
                high = mid;
            }
        }

        return ans;
    }
}
